package com.mrjy.maintain.bo;

import com.mrjy.common.entity.PolyInfo;
import com.mrjy.common.entity.UserLearning;
import com.mrjy.common.entity.UserVideoRel;
import com.mrjy.common.entity.VideoInfo;
import com.mrjy.common.util.PolyvTokenUtil;
import com.mrjy.common.util.PolyvUtil;
import com.mrjy.common.util.RedisUtil;
import com.mrjy.maintain.dao.IUserLearningInfoDao;
import com.mrjy.video.dao.IUserVideoRelDao;
import com.tencent.common.Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserLearningInfoBo implements IUserLearningInfoBo {
	
	@Resource
	private IUserLearningInfoDao userLearningInfoDao;
	@Resource
	private IUserVideoRelDao userVideoRelDao;

	@Override
	public Object queryUserLearning(UserLearning userLearning) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(userLearning.getUserId()==null){
			map.put("err_code", 400);
			map.put("err_msg", "参数异常");
			return map;
		}
		//加载视频列表
		UserVideoRel rel = new UserVideoRel();
		rel.setUserId(userLearning.getUserId());
		rel.setPage(userLearning.getPage());
		rel.setRows(userLearning.getRows());
		List<VideoInfo> videoList = userVideoRelDao.queryByUserId(rel);
		for (VideoInfo videoInfo : videoList) {
			if (RedisUtil.isVideoVip(rel.getUserId(), videoInfo.getId())) {
				videoInfo.setIsVipStatus("1");
			}else {
				videoInfo.setIsVipStatus("0");
			}

            PolyvTokenUtil tokenUtil = null;
            try {
                tokenUtil = PolyvTokenUtil.cloneSimplePolyInfo(videoInfo.getVideoCode(), videoInfo.getAudioCode(),
                        Util.getHostIp(Util.getInetAddress()),
                        userLearning.getUserId().toString(), "123", "1225");
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            videoInfo.setPolyvTokenUtil(tokenUtil);

            videoInfo.setAudioCode(null);
            videoInfo.setVideoCode(null);
        }
        PolyInfo polyInfo = null;
		if (videoList!=null && videoList.size()>0) {
			for (VideoInfo info : videoList) {
                if (info.getId() != null) {
                    polyInfo = RedisUtil.cache(info.getId() + "");
                    info.setPolyInfo(polyInfo);
                }
            }
		}
		//加载课程包列表
		List<UserLearning> coursepackageList = userLearningInfoDao.queryUserLearning(userLearning);
		//用户正在学的视频数量
		int videoCount = userLearningInfoDao.queryLearningVideoCount(userLearning);
		int coursepackageCount = userLearningInfoDao.queryLearningCoursePackageCount(userLearning);
		map.put("videoList", videoList);
		map.put("coursepackageList", coursepackageList);
		map.put("videoCount", videoCount);
		map.put("coursepackageCount", coursepackageCount);
		return map;
	}

	@Override
	public Object txInsert(UserLearning userLearning) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(userLearning.getUserId()==null||userLearning.getProgramId()==null){
			map.put("err_code", 400);
			map.put("err_msg", "参数异常");
			return map;
		}
		//查是否存在
		List<UserLearning> learning = userLearningInfoDao.queryByUserIdAndProgramId(userLearning);
		if (learning!=null && learning.size() > 0) {
			map.put("err_code", 401);
			map.put("err_msg", "用户已正在学该课程包");
			return map;
		}
		userLearning.setCreateTime(new Timestamp(System.currentTimeMillis()));
		int row = userLearningInfoDao.insert(userLearning);
		if (row > 0) {
			map.put("err_code", 0);
			map.put("err_msg", "添加成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "添加失败");
		}
		return map;
	}
	
	
}
